package com.xjtu.platform.security.mailFill;


import com.xjtu.platform.common.Result;
import com.xjtu.platform.common.Utils;
import com.xjtu.platform.security.forgetpassword.ForgetPasswordService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/security/mailFill")
public class MailFillAPI {

    @Resource
    private MailFillService mailFillService;

    @Resource
    private ForgetPasswordService forgetPasswordService;

    @PostMapping("/sendMail")
    public Result sendMail(@RequestBody MailDto mailDto) throws Exception {
        try {
            String code = forgetPasswordService.setRedisCode(mailDto.getUser_id());
            new Thread(()->{
                try {
                    Utils.sendMail(mailDto.getEmail(),"您的验证码为："+code+"\n有效期为10分钟");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
//            mailFillService.insertCode(code,mailDto);
            return Result.success("邮箱发送成功！！！");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(Result.ERR_CODE_SYS,"系统维护中.......");
        }


    }


    @PostMapping("/verifyCode")
    public Result verifyCode(@RequestBody MailDto mailDto){
        String code = forgetPasswordService.getRedisValue(mailDto.getUser_id());
        if(mailDto.getCode().equals(code)){
//            boolean ok = mailFillService.insertEmail(mailDto);
            return Result.success("恭喜邮箱完善成功！！！");
        }else {
            return Result.fail(Result.ERR_CODE_BUSINESS,"验证码错误....");
        }
    }
}
